package com.fieldchain.ex.fa100.ui.money.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.bean.CoinsBean;
import com.fieldchain.ex.fa100.bean.event.MainEvent;
import com.fieldchain.ex.fa100.ui.money.activity.BillActivity;
import com.fieldchain.ex.fa100.ui.money.activity.MarketChoseActivity;
import com.fieldchain.ex.fa100.ui.money.activity.RechargeActivity;
import com.fieldchain.ex.fa100.ui.money.activity.WithDrawActivity;
import com.fieldchain.ex.fa100.utils.screen.MyToast;
import com.bumptech.glide.Glide;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import com.fieldchain.ex.fa100.utils.string.NumberFormate;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/13
 */
public class MoneyAdapter extends RecyclerView.Adapter<MoneyAdapter.ViewHolder> {
    private Context mContext;
    private boolean tags[];
    private int newindex, oldindex;

    public MoneyAdapter(Context mContext) {
        this.mContext = mContext;
    }

    private List<CoinsBean.CoinListBean> list = new ArrayList<>();

    public void setData(List<CoinsBean.CoinListBean> list) {
        this.list.clear();
        this.list.addAll(list);
        oldindex = newindex = -1;
        tags = new boolean[list.size()];
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_money, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        final CoinsBean.CoinListBean bean = list.get(position);
        Glide.with(mContext).load(bean.getImage()).placeholder(R.drawable.icon_coinempty).into(holder.ivCoin);
        holder.tvCoin.setText(bean.getCoinName().toUpperCase());
        holder.tvNum1.setText(NumberFormate.m5(bean.getTotal()));
        holder.tvNum2.setText(NumberFormate.m5(bean.getUsable()));
        holder.tvNum3.setText(NumberFormate.m5(Double.parseDouble(bean.getFrozen())));

        if (tags[position]) {
            holder.linBot.setVisibility(View.VISIBLE);
        } else {
            holder.linBot.setVisibility(View.GONE);
        }

        holder.reaTop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                newindex = position;
                if (oldindex != newindex && oldindex >= 0) {
                    tags[oldindex] = false;
                }
                tags[newindex] = !tags[newindex];

                if (tags[newindex] && oldindex >= 0) {
                    notifyItemChanged(oldindex);
                }
                notifyItemChanged(newindex);
                oldindex = newindex;
            }
        });

        holder.tvJp1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (bean.getCoinType() == 3) {
                    EventBus.getDefault().post(new MainEvent(3, 0, list.get(position).getCoinName()));
                } else if(bean.getCoinType()==4){
                    MyToast.show("此平台币暂不可充值");
                }else {
                    Intent intent = new Intent(mContext, RechargeActivity.class);
                    intent.putExtra("coin", list.get(position).getCoinName());
                    intent.putExtra("coinId", bean.getId());
                    mContext.startActivity(intent);
                }
            }
        });
        holder.tvJp2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (bean.getCoinType() == 3) {
                    EventBus.getDefault().post(new MainEvent(3, 1, list.get(position).getCoinName()));
                } else if(bean.getCoinType()==4){
                    MyToast.show("此平台币暂不可提币");
                } else {
                    Intent intent = new Intent(mContext, WithDrawActivity.class);
                    intent.putExtra("coin", bean.getCoinName());
                    intent.putExtra("coinId", bean.getId());
                    intent.putExtra("num", bean.getUsable());
                    mContext.startActivity(intent);
                }
            }
        });
        holder.tvJp3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                if (bean.getCoinType() == 3) {
//                    EventBus.getDefault().post(new MainEvent(3, 2, list.get(position).getCoinName()));
//                } else {
                    Intent intent = new Intent(mContext, BillActivity.class);
                    intent.putExtra("coin", bean.getCoinName());
                    intent.putExtra("coinId", bean.getId());
                    intent.putExtra("num", bean.getUsable());
                    mContext.startActivity(intent);
//                }
            }
        });
        holder.tvJp4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, MarketChoseActivity.class);
                intent.putExtra("coin", bean.getCoinName());
                ((Activity) mContext).startActivityForResult(intent, 0);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private RelativeLayout reaTop;
        private ImageView ivCoin;
        private TextView tvCoin;
        private TextView tvNum1;
        private LinearLayout linBot;
        private TextView tvNum2;
        private TextView tvNum3;
        private TextView tvJp1;
        private TextView tvJp2;
        private TextView tvJp3;
        private TextView tvJp4;

        public ViewHolder(View itemView) {
            super(itemView);
            reaTop = itemView.findViewById(R.id.rea_top);
            ivCoin = itemView.findViewById(R.id.iv_coin);
            tvCoin = itemView.findViewById(R.id.tv_coin);
            tvNum1 = itemView.findViewById(R.id.tv_num1);
            linBot = itemView.findViewById(R.id.lin_bot);
            tvNum2 = itemView.findViewById(R.id.tv_num2);
            tvNum3 = itemView.findViewById(R.id.tv_num3);
            tvJp1 = itemView.findViewById(R.id.tv_jp1);
            tvJp2 = itemView.findViewById(R.id.tv_jp2);
            tvJp3 = itemView.findViewById(R.id.tv_jp3);
            tvJp4 = itemView.findViewById(R.id.tv_jp4);
        }
    }
}
